package com.wm.task1.dao;

import com.wm.task1.util.LinkDB;
import com.wm.task1.vo.Massage;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class MessageDAO {

    private List<Massage> massageList = new ArrayList<>();

    private Scanner sc = new Scanner(System.in);
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/demosys";
    private static final String USER = "root";
    private static final String PASS = "123456";

    public MessageDAO() {
    }

//    public List<Massage> lookMassage() { //查看留言
//        return LinkDB.linkDb();
//    }

//   public void addMassage(String name,String countent) { //添加留言
//       LinkDB.addMassage(name,countent);
//   }



    public List<Massage> lookMassage(){
        ResultSet rs = null;
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(URL,USER,PASS);

            PreparedStatement ps = connection.prepareStatement("SELECT * FROM massage");
            rs = ps.executeQuery();
            while (rs.next()){
                massageList.add(new Massage(rs.getInt("id"),
                        rs.getString("name"),rs.getString("content")));

            }
            rs.close();
            ps.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return massageList;
    }
    public void addMassage(String name,String countent){
        //INSERT INTO `demosys`.`massage` (`name`, `content`) VALUES ('王五', '李明骏真丑');massage
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(URL,USER,PASS);

            PreparedStatement ps = connection.prepareStatement
                    ("INSERT INTO `demosys`.`massage` (`name`, `content`) VALUES ('"+name+"', '"+countent+"');");
            ps.executeUpdate();
            ps.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public List<Massage> findMassage(String name) {  //查找留言
        ResultSet rs = null;
        try {
            Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(URL,USER,PASS);

            PreparedStatement ps = connection.prepareStatement("SELECT * FROM massage WHERE NAME='"+name+"';");
            rs = ps.executeQuery();
            while (rs.next()){
                massageList.add(new Massage(rs.getInt("id"),
                        rs.getString("name"),rs.getString("content")));

            }
            rs.close();
            ps.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return massageList;


    }

}
